1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.collect.testing.google;
16  
17  import static com.google.common.collect.testing.Helpers.copyToList;
18  import static com.google.common.collect.testing.Helpers.copyToSet;
19  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
21  import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
22  
23  import com.google.common.annotations.GwtCompatible;
24  import com.google.common.collect.SetMultimap;
25  import com.google.common.collect.testing.features.CollectionSize;
26  import com.google.common.collect.testing.features.MapFeature;
27  
28  import java.util.List;
29  import java.util.Map.Entry;
30  import java.util.Set;
31  
32  /**
33   * Tests for {@link SetMultimap#replaceValues}.
34   *
35   * @author Louis Wasserman
36   */
37  @GwtCompatible
38  public class SetMultimapPutTester<K, V>
39      extends AbstractMultimapTester<K, V, SetMultimap<K, V>> {
40    // Tests for non-duplicate values are in MultimapPutTester
41  
42    @MapFeature.Require(SUPPORTS_PUT)
43    @CollectionSize.Require(absent = ZERO)
44    public void testPutDuplicateValuePreservesSize() {
45      assertFalse(multimap().put(sampleKeys().e0, sampleValues().e0));
46      assertEquals(getNumElements(), multimap().size());
47    }
48  
49    @MapFeature.Require(SUPPORTS_PUT)
50    public void testPutDuplicateValue() {
51      List<Entry<K, V>> entries = copyToList(multimap().entries());
52  
53      for (Entry<K, V> entry : entries) {
54        resetContainer();
55        K k = entry.getKey();
56        V v = entry.getValue();
57  
58        Set<V> values = multimap().get(k);
59        Set<V> expectedValues = copyToSet(values);
60  
61        assertFalse(multimap().put(k, v));
62        assertEquals(expectedValues, values);
63        assertGet(k, expectedValues);
64      }
65    }
66  
67    @MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
68    @CollectionSize.Require(absent = ZERO)
69    public void testPutDuplicateValue_null() {
70      initMultimapWithNullValue();
71      assertFalse(multimap().put(getKeyForNullValue(), null));
72      expectContents(createArrayWithNullValue());
73    }
74  }